//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LossDistBucketing.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/LossDist.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CLossDistBucketing::CLossDistBucketing (UInt64 nBuckets, Double maximum, Microsoft::FSharp::Core::FSharpOption<Double>^ epsilon) : CLossDist(CLossDistBucketing::typeid)
{
    try
    {
#ifdef HANDLE
        _phLossDistBucketing = NULL;
#endif
        QuantLib::Size _nBuckets = (QuantLib::Size)ValueHelper::Convert (nBuckets);
        QuantLib::Real _maximum = (QuantLib::Real)ValueHelper::Convert (maximum);
        QuantLib::Real _epsilon = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (epsilon) ? (QuantLib::Real)ValueHelper::Convert (epsilon->Value) : 1e-6); //4
        _ppLossDistBucketing = new boost::shared_ptr<QuantLib::LossDistBucketing> (new QuantLib::LossDistBucketing ( _nBuckets,  _maximum,  _epsilon ));
        SetLossDist (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistBucketing));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::CLossDistBucketing::CLossDistBucketing (boost::shared_ptr<QuantLib::LossDistBucketing>& childNative, Object^ owner) : CLossDist(CLossDistBucketing::typeid)
{
#ifdef HANDLE
	_phLossDistBucketing = NULL;
#endif
	_ppLossDistBucketing = &childNative;
    _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistBucketing));
}
Cephei::QL::Experimental::Credit::CLossDistBucketing::CLossDistBucketing (QuantLib::LossDistBucketing& childNative, Object^ owner) : CLossDist(CLossDistBucketing::typeid)
{
#ifdef HANDLE
	_phLossDistBucketing = NULL;
#endif
	_ppLossDistBucketing = new boost::shared_ptr<QuantLib::LossDistBucketing> (&childNative);
    _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistBucketing));
    _LossDistBucketingOwner = owner;
    _LossDistOwner = owner;
}

Cephei::QL::Experimental::Credit::CLossDistBucketing::CLossDistBucketing (CLossDistBucketing^ copy) : CLossDist(CLossDistBucketing::typeid)
{
#ifdef HANDLE
	_phLossDistBucketing = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLossDistBucketing = new boost::shared_ptr<QuantLib::LossDistBucketing> (copy->GetShared());
        _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistBucketing));
    }
}
Cephei::QL::Experimental::Credit::CLossDistBucketing::CLossDistBucketing (System::Type^ t) : CLossDist(CLossDistBucketing::typeid)
{
#ifdef HANDLE
	_phLossDistBucketing = NULL;
#endif
	if (!t->IsSubclassOf(CLossDistBucketing::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CLossDistBucketing::CLossDistBucketing (QuantLib::Handle<QuantLib::LossDistBucketing>& childNative, Object^ owner)  : CLossDist(CLossDistBucketing::typeid)
{
	_phLossDistBucketing = &childNative;
	_ppLossDistBucketing = &static_cast<boost::shared_ptr<QuantLib::LossDistBucketing>>(childNative.currentLink());
    _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistBucketing));
    _LossDistBucketingOwner = owner;
}
Cephei::QL::Experimental::Credit::CLossDistBucketing::CLossDistBucketing (QuantLib::Handle<QuantLib::LossDistBucketing> childNative)  : CLossDist(CLossDistBucketing::typeid)
{
	_phLossDistBucketing = &childNative;
	_ppLossDistBucketing = &static_cast<boost::shared_ptr<QuantLib::LossDistBucketing>>(childNative.currentLink());
    _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistBucketing));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CLossDistBucketing::CLossDistBucketing (QuantLib::LossDistBucketing childNative)  : CLossDist(CLossDistBucketing::typeid)
{
#ifdef HANDLE
	_phLossDistBucketing = NULL;
#endif
	_ppLossDistBucketing = new boost::shared_ptr<QuantLib::LossDistBucketing> (new QuantLib::LossDistBucketing (childNative));
    _ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (boost::dynamic_pointer_cast<QuantLib::LossDist> (*_ppLossDistBucketing));
}
#endif

Cephei::QL::Experimental::Credit::CLossDistBucketing::~CLossDistBucketing ()
{
    if (_ppLossDistBucketing != NULL)
    {
	    delete _ppLossDistBucketing;
        _ppLossDistBucketing = NULL;
    }
}
Cephei::QL::Experimental::Credit::CLossDistBucketing::!CLossDistBucketing ()
{
    if (_ppLossDistBucketing != NULL)
    {
	    delete _ppLossDistBucketing;
    }
}
QuantLib::LossDistBucketing& Cephei::QL::Experimental::Credit::CLossDistBucketing::GetReference ()
{
    if (_ppLossDistBucketing == NULL) throw gcnew NativeNullException ();
	return **_ppLossDistBucketing;
}
boost::shared_ptr<QuantLib::LossDistBucketing>& Cephei::QL::Experimental::Credit::CLossDistBucketing::GetShared ()
{
    if (_ppLossDistBucketing == NULL) throw gcnew NativeNullException ();
	return *_ppLossDistBucketing;
}
QuantLib::LossDistBucketing* Cephei::QL::Experimental::Credit::CLossDistBucketing::GetPointer ()
{
    if (_ppLossDistBucketing == NULL) throw gcnew NativeNullException ();
	return &**_ppLossDistBucketing;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LossDistBucketing>& Cephei::QL::Experimental::Credit::CLossDistBucketing::GetHandle ()
{
	if (_phLossDistBucketing == NULL)
	{
		_phLossDistBucketing = new Handle<QuantLib::LossDistBucketing> (*_ppLossDistBucketing);
	}
	return *_phLossDistBucketing;
}
#endif
bool Cephei::QL::Experimental::Credit::CLossDistBucketing::HasNative () 
{
	return (_ppLossDistBucketing != NULL);
}

UInt64 Cephei::QL::Experimental::Credit::CLossDistBucketing::Buckets::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLossDistBucketing)->buckets ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CLossDistBucketing::Maximum::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLossDistBucketing)->maximum ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::ILossDistBucketing^ Cephei::QL::Experimental::Credit::CLossDistBucketing_Factory::Create (UInt64 nBuckets, Double maximum, Microsoft::FSharp::Core::FSharpOption<Double>^ epsilon)
{
    return gcnew CLossDistBucketing ( nBuckets,  maximum,  epsilon);
}
